#ifndef SoftwareSPI_h
#define SoftwareSPI_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <SPI.h>

class SoftwareSPI : public SPIClass
{
public:
    SoftwareSPI(uint8_t pin_mosi, uint8_t pin_miso, uint8_t pin_sck, uint8_t pin_cs, uint8_t cpha = 1, uint8_t cpol = 1);
    void begin(uint8_t _pin = CS_PIN_CONTROLLED_BY_USER);
    void end(void);
    uint8_t transfer(uint8_t);
    void select();
    void deselect();
    void beginTransaction(uint8_t pin, SPISettings settings){};
    void endTransaction(uint8_t pin){};
    byte transfer(uint8_t pin, uint8_t _data, SPITransferMode _mode = SPI_LAST) { return transfer(_data); };
    void transfer(void *_buf, size_t _count, SPITransferMode _mode = SPI_LAST)
    {
        uint8_t *buf = (uint8_t *)_buf;
        for (int i = 0; i < _count; i++)
            buf[i] = transfer(buf[i]);
    }
    void transfer(void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode = SPI_LAST)
    {
        uint8_t *bufout = (uint8_t *)_bufout;
        uint8_t *bufin = (uint8_t *)_bufin;
        for (int i = 0; i < _count; i++)
            bufin[i] = transfer(bufout[i]);
    }

private:
    uint8_t _pin_sck;
    uint8_t _pin_mosi;
    uint8_t _pin_miso;
    uint8_t _pin_cs;
    uint8_t _cpha, _cpol;
};

#endif
